# Sustainable Office Designer
# by Fabian Ritter <Fabian.Ritter@live.de>, 2012

require("SOD/tools/dialogs/dialog.rb")
require("SOD/ProjectManager.rb")

module SOD
  
  class VersioningDialog < SOD_Dialog # see OS Dialog
    
    def initialize(interface, title = "Default", scrollbars = true, w = 800, h = 580, l = 150, t = 150)
      super
      set_file("versioning.html")
      add_callbacks
    end
    
    def update
      
      @projectName = Plugin.projectManager.activeProjectName
      @filePath = Plugin.projectManager.activeFilePath.to_s
      @var1 = Plugin.projectManager.activeFileName
      puts @var1 if $debug
      if @var1.nil?
        @var1 = "No File Loaded!"
      end
      
      set_element_value("PROJECT", @projectName)
      set_element_value("PATH", @filePath)
      set_element_value("VARIANT_1", @var1)
      
      fileNames = Plugin.projectManager.projectFileList
      fileNames.remove(@var1.to_s)
      
      set_select_options("VARIANT_2", fileNames.to_a)
      
      set_element_value("VAR1_VC", Plugin.model.projectValues[:tot_vc].round.to_s) # volume concrete
      set_element_value("VAR1_MR", (Plugin.model.projectValues[:tot_mr]/1000).round.to_s) # mass reinforcement steel
      set_element_value("VAR1_MP", (Plugin.model.projectValues[:tot_mp]/1000).round.to_s) # mass profiled sheeting
      set_element_value("VAR1_MQ", (Plugin.model.projectValues[:tot_mq]/1000).round.to_s) # mass structure steel
      set_element_value("VAR1_AA", (Plugin.model.projectValues[:tot_aa]/1000).round.to_s) # area steel coating
    end
    
    def on_change_element(d, p)
      # problem here:  assumes value does not have any commas in it, need to just split off first one
      args = p.split(",")
      key = args[0]
      value = args[1]
      
      path = @filePath + "/" + value + ".xml"
      
      case(key)
      when "VARIANT_2"
        building, values = Plugin.projectManager.fileManager.read_values( path )
        
        1.upto(4) { |i| values[i] = values[i]/1000 }
        
        set_element_value("VAR2_VC", values[0].round.to_s) # volume concrete
        set_element_value("VAR2_MR", values[1].round.to_s) # mass reinforcement steel
        set_element_value("VAR2_MP", values[2].round.to_s) # mass profiled sheeting
        set_element_value("VAR2_MQ", values[3].round.to_s) # mass structure steel
        set_element_value("VAR2_AA", values[4].round.to_s) # area steel coating
        
        values << Plugin.model.projectValues[:tot_vc]
        values << Plugin.model.projectValues[:tot_mr]/1000 # to_tons
        values << Plugin.model.projectValues[:tot_mp]/1000 # to_tons
        values << Plugin.model.projectValues[:tot_mq]/1000 # to_tons
        values << Plugin.model.projectValues[:tot_aa]/1000 # to 1000 m^2
        
        scaled_values = scale_values( values )
        
        values_arg = ""
        
        if (not scaled_values.empty?)
          for i in 0...scaled_values.length
            if (scaled_values[i].nil?)
              scaled_values[i] = ""
            else
              scaled_values[i] = scaled_values[i].to_s.gsub(/'/, "%apos%")  # Encode any single quotes so that a JavaScript error is avoided
            end
            values_arg += scaled_values[i] + ","
          end
          values_arg = values_arg[0...values_arg.length - 1]
        end
        
        values_arg = values_arg.gsub(/,/, "%comma%")
        
        puts values_arg if $debug
        
        @webDialog.execute_script("SchreibeTabelle('" + values_arg + "')")
        
      else
        puts "ProjectInfo: No matching Value returned!" if $debug
      end
      
    end
    
    def scale_values( values )
      max = values[0]
      values.each { |val| max = val if  max < val }
      print "unscaled: "if $debug
      values.each { |val| print " #{val}" } if $debug
      print "\n"
      
      scaled_values = []
      values.each { |val| scaled_values << (val/max * 200) }
      print "scaled: " if $debug
      scaled_values.each { |val| print " #{val}" } if $debug
      print "\n"
      
      return scaled_values
    end
    
  end
  
end
